package utils

import (
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"os"
	"time"
)

var sugarLogger *zap.SugaredLogger

func InitLogger() *zap.Logger {
	writer := getLogWriter()
	encoder := getEncoder()
	core := zapcore.NewCore(encoder, writer, zapcore.DebugLevel)
	logger := zap.New(core)
	zap.ReplaceGlobals(logger)
	return logger
}

func getEncoder() zapcore.Encoder {
	encoder := zap.NewProductionEncoderConfig()
	encoder.EncodeTime = zapcore.TimeEncoderOfLayout(time.DateTime) // 自定义时间格式

	return zapcore.NewJSONEncoder(encoder)
}

func getLogWriter() zapcore.WriteSyncer {
	create, _ := os.OpenFile("./app.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 06666)
	return zapcore.AddSync(create)

}
